package com.example.monitor;

import com.example.utils.HiveUtils;
import lombok.extern.slf4j.Slf4j;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Slf4j
public class ConnMonitorThread implements Runnable{

    private String url;

    private String user;

    private String password;



    public ConnMonitorThread(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }
    public void run() {
        try {
            System.out.println("~~~~~~~~~~~~~~ConnMonitorThread~~~~~~~~~~~~~!!!!");
            String sql = "select 1 from dual";
            Statement statement = HiveUtils.conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            /*while (resultSet.next()) {
            }*/
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            System.out.println("捕获到了异常！！！！！！！！！！！！");
            e.printStackTrace();
            log.error("hive数据库连接中断，尝试重连" + e.getMessage());
            try {
                HiveUtils.conn = null;
                HiveUtils.conn = DriverManager.getConnection(HiveUtils.url, HiveUtils.user, HiveUtils.password);
            } catch (SQLException e1) {
                log.error("重连hive数据库失败：" + e.getMessage());
                e1.printStackTrace();
            }
        }
    }
}
